export function img2base64(filePath, onEncodeSuccess) {
	wx.getFileSystemManager().readFile({
		filePath, //选择图片返回的相对路径
		encoding: 'base64', //编码格式

		success: res => { //成功的回调
			console.log("img2base64 success")
			onEncodeSuccess(res.data)
		},

		fail(err) {
			console.log("getOcrRimg2base64esult fail", err)
		}
	})
}

export function getOcrDataObj(imgBase64, cb) {
	uni.request({
		// url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=24.21cb42c3facb89e888e949424254fb5a.2592000.1687042768.282335-33719282',
		url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=24.21cb42c3facb89e888e949424254fb5a.2592000.1687042768.282335-33719282',
		data: {
			image: imgBase64,
			detect_direction: true, //检测图像朝向
			// language_type: 'ENG', //识别语言类型，默认中英文结合
		},
		method: 'POST',
		header: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		success: (res) => {
			console.log("getOcrResult success", res.data)
			cb(res.data)
		},
		fail(err) {
			console.log("getOcrResult fail", err)
		}
	})
}

export function getOcrResult(filePath, onOcrResult) {
	img2base64(
		// res.tempFilePaths[0],
		filePath,

		imgBase64 => {
			getOcrDataObj(
				imgBase64,
				dataObj => {
					let ocrResult = dataObj.words_result.reduce(
						(pv, cv, i) => pv + cv.words,
						""
					)
					
					onOcrResult(ocrResult)
				}
			)
		}
	)
}